<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="订单编号" prop="outTradeNo">
        <el-input
          v-model="queryParams.outTradeNo"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="交易单号" prop="transactionId">
        <el-input
          v-model="queryParams.transactionId"
          placeholder="请输入交易单号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择支付状态" clearable>
          <el-option label="未支付" value="0" />
          <el-option label="已支付" value="1" />
          <el-option label="已取消" value="2" />
          <el-option label="已退款" value="3" />
        </el-select>
      </el-form-item>
      <el-form-item label="支付时间">
        <el-date-picker
          v-model="dateRange"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['member:order:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList">
      <el-table-column label="订单ID" align="center" prop="orderId" width="80" />
      <el-table-column label="订单编号" align="center" prop="outTradeNo" min-width="200" :show-overflow-tooltip="true" />
      <el-table-column label="交易单号" align="center" prop="transactionId" min-width="250" :show-overflow-tooltip="true" />
      <el-table-column label="会员ID" align="center" prop="memberId" width="80" />
      <el-table-column label="订单金额" align="center" prop="totalAmount" width="100">
        <template #default="scope">
          <span style="color: #f56c6c; font-weight: bold;">￥{{ scope.row.totalAmount }}</span>
        </template>
      </el-table-column>
      <el-table-column label="支付状态" align="center" prop="status" width="100">
        <template #default="scope">
          <el-tag v-if="scope.row.status === '0'" type="info">未支付</el-tag>
          <el-tag v-else-if="scope.row.status === '1'" type="success">已支付</el-tag>
          <el-tag v-else-if="scope.row.status === '2'" type="warning">已取消</el-tag>
          <el-tag v-else-if="scope.row.status === '3'" type="danger">已退款</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="支付时间" align="center" prop="paidAt" width="160">
        <template #default="scope">
          <span>{{ parseTime(scope.row.paidAt) || '-' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="160">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150" fixed="right">
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Refresh"
            @click="handleSync(scope.row)"
            v-hasPermi="['member:order:sync']"
          >同步状态</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script setup name="MemberOrder">
import { listOrder, syncOrderStatus } from "@/api/member/order";
import { getCurrentInstance, ref, reactive, toRefs } from 'vue';

const { proxy } = getCurrentInstance();

const orderList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);
const dateRange = ref([]);

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    outTradeNo: null,
    transactionId: null,
    status: null
  }
});

const { queryParams } = toRefs(data);

/** 查询订单列表 */
function getList() {
  loading.value = true;
  const params = proxy.addDateRange(queryParams.value, dateRange.value);
  listOrder(params).then(response => {
    orderList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 同步订单状态 */
function handleSync(row) {
  proxy.$modal.confirm('是否同步订单"' + row.outTradeNo + '"的支付状态？').then(function() {
    return syncOrderStatus(row.outTradeNo);
  }).then(() => {
    proxy.$modal.msgSuccess("同步成功");
    getList();
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('member/order/export', {
    ...queryParams.value
  }, `member_order_${new Date().getTime()}.xlsx`)
}

getList();
</script>